/*
 * @Description: 聊天页面
 * @Author: iamsmiling
 * @Date: 2021-06-01 21:26:04
 * @LastEditTime: 2021-06-15 14:38:26
 */

import 'package:autolist/autolist.dart';
import 'package:credit_business_circle/R/R.dart';
import 'package:credit_business_circle/components/modal/message_board_modal.dart';
import 'package:credit_business_circle/domain/remote/message/chat_record_entity.dart';
import 'package:credit_business_circle/pages/message/children/chat/widget/chat_bubble.dart';
import 'package:credit_business_circle/routes/app_routes.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'chat_controller.dart';

class ChatPage extends GetView<ChatController> {
  const ChatPage({Key? key}) : super(key: key);

  Future openDialog(BuildContext ctx) {
    RenderBox renderBox = ctx.findRenderObject() as RenderBox;
    Rect box = renderBox.localToGlobal(Offset.zero) & renderBox.size;
    double top = GetPlatform.isAndroid ? box.center.dy : box.topCenter.dy;

    print(box.size);
    return showDialog(
        context: ctx,
        barrierColor: R.color.transparent,
        builder: (BuildContext context) {
          return DefaultTextStyle(
            style: TextStyle(
                fontSize: R.dimen.sp16,
                color: R.color.ffffffff,
                fontWeight: FontWeight.w500),
            child: Stack(
              alignment: AlignmentDirectional.topCenter,
              children: [
                Positioned(
                  top: top - R.dimen.dp6,
                  right: R.dimen.width - box.bottomCenter.dx - R.dimen.dp9,
                  child: Container(
                    child: Column(
                      children: [
                        Container(
                            decoration: BoxDecoration(
                                color: R.color.ff4c4c4c,
                                borderRadius:
                                    BorderRadius.circular(R.dimen.sp5)),
                            child: Column(
                              children: [
                                Container(
                                  decoration: BoxDecoration(
                                      border: Border(
                                          bottom: BorderSide(
                                              width: .5,
                                              color: R.color.ff777777))),
                                  padding: EdgeInsets.symmetric(
                                      horizontal: R.dimen.dp28,
                                      vertical: R.dimen.dp18),
                                  child: Row(
                                    children: [
                                      Container(
                                        margin: EdgeInsets.only(
                                            right: R.dimen.dp10),
                                        child: Image.asset(
                                            R.image.dragIntoBlackList),
                                      ),
                                      Text("屏蔽拉黑")
                                    ],
                                  ),
                                ),
                                GestureDetector(
                                  onTap: () => Get.toNamed(AppRoutes.report),
                                  child: Container(
                                    padding: EdgeInsets.symmetric(
                                        horizontal: R.dimen.dp28,
                                        vertical: R.dimen.dp18),
                                    child: Row(
                                      children: [
                                        Container(
                                          margin: EdgeInsets.only(
                                              right: R.dimen.dp10),
                                          child: Image.asset(R.image.report),
                                        ),
                                        Text("广告举报")
                                      ],
                                    ),
                                  ),
                                ),
                              ],
                            ))
                      ],
                    ),
                  ),
                ),
                Positioned(
                  top: top - R.dimen.dp12,
                  right: R.dimen.width - box.bottomCenter.dx,
                  child: Image.asset(R.image.triangle),
                ),
              ],
            ),
          );
          // return BubbleDialog(
          //     arrowLocation: ArrowLocation.TOP,
          //     x: getX(ctx) + getWidth(ctx) / 2,
          //     y: getY1(ctx) - R.dimen.dp16,
          //     width: R.dimen.dp142,
          //     height: R.dimen.dp116,
          //     child: Container(
          //       child: DefaultTextStyle(
          //         style: TextStyle(
          //             fontSize: R.dimen.sp16,
          //             color: R.color.ffffffff,
          //             fontWeight: FontWeight.w500),
          //         child: Column(
          //           children: [
          //             Container(
          //               alignment: Alignment.center,
          //               height: R.dimen.dp54,
          //               child: Row(
          //                 mainAxisAlignment: MainAxisAlignment.center,
          //                 crossAxisAlignment: CrossAxisAlignment.center,
          //                 children: [
          //                   Container(
          //                     margin: EdgeInsets.only(right: R.dimen.dp13),
          //                     child: Image.asset(R.image.dragIntoBlackList),
          //                   ),
          //                   Text("屏蔽拉黑")
          //                 ],
          //               ),
          //             ),
          //             Divider(color: R.color.ff777777),
          //             Container(
          //               alignment: Alignment.center,
          //               height: R.dimen.dp48,
          //               child: Row(
          //                 mainAxisAlignment: MainAxisAlignment.center,
          //                 crossAxisAlignment: CrossAxisAlignment.center,
          //                 children: [
          //                   Container(
          //                     margin: EdgeInsets.only(right: R.dimen.dp13),
          //                     child: Image.asset(R.image.report),
          //                   ),
          //                   Text("广告举报")
          //                 ],
          //               ),
          //             )
          //           ],
          //         ),
          //       ),
          //     ));
        });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: R.color.fff1f1f1,
      resizeToAvoidBottomInset: true,
      appBar: AppBar(
        title: Text("不会聊天的鱼"),
        elevation: 0,
        actions: [
          Builder(builder: (BuildContext ctx) {
            return IconButton(
                icon: Image.asset(R.image.moreAction),
                onPressed: () => openDialog(ctx));
          })
        ],
      ),
      body: Container(
        margin:
            EdgeInsets.only(bottom: kBottomNavigationBarHeight + R.dimen.dp20),
        child: GetBuilder<ChatController>(builder: (_) {
          return AutoList(
            duration: const Duration(milliseconds: 375),
            items: controller.records,
            controller: controller.scrollController,
            itemBuilder: (BuildContext context, ChatRecordEntity record) {
              return ChatBubble(record: record);
            },
          );
        }),
      ),
      bottomSheet: Container(
          // height: kBottomNavigationBarHeight,
          margin: EdgeInsets.only(
              left: R.dimen.dp11,
              right: R.dimen.dp11,
              top: R.dimen.dp13,
              bottom: R.dimen.dp14),
          child: GestureDetector(
            onTap: () => showMessageBoardModal(context,
                onMessageTap: controller.sendMessage),
            child: Row(
              children: [
                Container(
                  margin: EdgeInsets.only(right: R.dimen.dp9),
                  padding: EdgeInsets.all(R.dimen.dp7),
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(R.dimen.sp3),
                      gradient: LinearGradient(
                          colors: [R.color.ff1bbd8d, R.color.ff14ca94])),
                  child: Text(
                    "常用语",
                    style: TextStyle(
                        fontSize: R.dimen.sp11, color: R.color.ffffffff),
                  ),
                ),
                Expanded(
                  child: Container(
                    padding: EdgeInsets.symmetric(horizontal: R.dimen.dp6),
                    decoration: BoxDecoration(
                        border: Border.all(color: R.color.ffcccccc),
                        borderRadius: BorderRadius.circular(R.dimen.sp3)),
                    child: TextFormField(
                      onTap: controller.jumpToBottom,
                      controller: controller.editingController,
                      onFieldSubmitted: controller.sendMessage,
                      decoration: InputDecoration(
                          border: InputBorder.none,
                          enabledBorder: InputBorder.none,
                          focusedBorder: InputBorder.none,
                          hintStyle: TextStyle(
                              color: R.color.ffaaaaaa, fontSize: R.dimen.sp12),
                          hintText: "留言内容严禁留联系方式，违规者将做严厉处罚"),
                    ),
                  ),
                ),
                Container(
                  padding: EdgeInsets.symmetric(horizontal: R.dimen.dp10),
                  child: Image.asset(R.image.smileEmoji),
                ),
              ],
            ),
          )),
    );
  }
}
